home *** CD-ROM | disk | FTP | other *** search
- #include <MacHeaders>
- #include <Palettes.h>
- #include <PictUtil.h>
- #include <stdio.h>
- #include <string.h>
- #include "sample shell.h"
- #include "sample utilities.h"
-
-
- #define colorsToRequest 16
- #define standardClutID 4
-
-
- MenuHandle myMenus[3];
- Boolean quitting = false;
-
-
- typedef struct windowData {
- diskPicture contentPicture;
- } windowData;
-
-
- void MakeNewWindow(void);
-
-
- void main(void)
- {
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- MaxApplZone();
-
- if( GetResource('MENU', fileID)==0 ) {
- SysBeep(20);
- CantOpen();
- return;
- }
-
- SetUpMenus();
- SetUpWindows();
-
- while( !quitting )
- MainEvent();
- }
-
-
- void MainEvent(void)
- {
- EventRecord myEvent;
- WindowPtr whichWindow;
- short windowPart;
-
- MaintainMenus();
- SystemTask();
- if( GetNextEvent(everyEvent, &myEvent) ) {
- switch( myEvent.what ) {
- case mouseDown:
- windowPart = FindWindow(myEvent.where, &whichWindow);
- DoMouseDown(windowPart, whichWindow, &myEvent);
-
- case keyDown:
- case autoKey:
- {
- register char theChar;
-
- theChar = myEvent.message & charCodeMask;
- if ((myEvent.modifiers & cmdKey) != 0)
- DoCommand( MenuKey(theChar) );
- break;
- }
-
- case activateEvt:
- break;
-
- case updateEvt:
- UpdateWindow((WindowPtr)myEvent.message);
- break;
- }
- }
- }
-
-
- void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
- {
- switch( windowPart ) {
- case inGoAway:
- if( TrackGoAway(whichWindow, myEvent->where) )
- DoFile(fmClose);
- break;
-
- case inMenuBar:
- DoCommand( MenuSelect(myEvent->where) );
- break;
-
- case inSysWindow:
- SystemClick(myEvent, whichWindow);
- break;
-
- case inDrag:
- { Rect dragRect = screenBits.bounds;
- InsetRect(&dragRect, 2, 2);
- DragWindow(whichWindow, myEvent->where, &dragRect);
- break;
- }
-
- case inGrow:
- break;
-
- case inContent:
- if( whichWindow != FrontWindow() )
- SelectWindow(whichWindow);
- else
- DoContent(whichWindow, myEvent);
- break;
- }
- }
-
-
- void DoContent(WindowPtr whichWindow, EventRecord *myEvent)
- {
- }
-
-
- void DoCommand(long mResult)
- {
- short theItem;
- Str255 name;
-
- theItem = LoWord(mResult);
- switch( HiWord(mResult) ) {
- case appleID:
- if( theItem > 2 ) {
- CGrafPtr savePort;
- GetItem(myMenus[appleM], theItem, &name);
- GetPort(&savePort);
- OpenDeskAcc(name);
- SetPort(savePort);
- }
- break;
-
- case fileID:
- DoFile(theItem);
- break;
-
- case editID:
- if( SystemEdit(theItem-1) == 0 )
- DoEdit(theItem);
- break;
- }
-
- HiliteMenu(0);
- }
-
-
- pascal short InitOctree(short colorsRequested, long *dataHandlePtr, short *bankType);
- pascal short RecordOctreeColors(long dataHandle, RGBColor *colorPtr, long colorCount, long *uniqueColorsPtr);
- pascal short KillOctree(long dataHandle);
-
-
- void DoFile(short item)
- {
- switch( item ) {
-
- case fmOpen:
- case fmNew:
- DoOpen();
- break;
-
- case fmClose:
- { WindowPtr currentWindow;
- if( currentWindow = FrontWindow() ) {
- windowData **dataHandle = (windowData **)GetWRefCon(currentWindow);
- PaletteHandle tempPalette;
-
- /* note that we lock the data handle, since DisposeDiskPicture might move memory */
- HLock(dataHandle);
- DisposeDiskPicture((*dataHandle)->contentPicture);
- HUnlock(dataHandle);
- DisposeHandle(dataHandle);
-
- /* note that we don’t dispose of the palette until after the window is disposed of, since the window
- may need it during the dispose process */
- tempPalette = GetPalette(currentWindow);
- DisposeWindow(currentWindow);
- DisposePalette(tempPalette);
- }
- break;
- }
-
- case fmQuit:
- while( FrontWindow() )
- DoFile(fmClose);
- quitting = true;
- break;
- }
- }
-
-
- void DoEdit(short item)
- {
- }
-
-
- void DoOpen(void)
- {
- Rect windowBounds, pictureBounds;
- StandardFileReply reply;
- SFTypeList typeList;
- WindowPtr tempWindow;
- windowData **dataHandle;
- diskPicture contentPicture;
- OSErr error;
- PictInfo resultInfo;
- char windowName[256];
- PaletteHandle originalPalette;
-
- typeList[0] = 'PICT';
- StandardGetFile(nil, 1, typeList, &reply);
- if( reply.sfGood == false )
- return;
-
- // MakeITable(colorTab, inverseTab, 5);
-
- contentPicture = NewDiskPicture(&reply.sfFile);
- GetDiskPictureBounds(contentPicture, &pictureBounds);
- FindScreen(screen8Bit + screenColor, pictureBounds.right - pictureBounds.left,
- pictureBounds.bottom - pictureBounds.top, &windowBounds);
-
- /* this is for the unmatched one */
- dataHandle = (windowData **)NewHandle(sizeof(windowData));
- HLock(dataHandle);
- (*dataHandle)->contentPicture = contentPicture;
-
- sprintf(&windowName[1], "%#s : original", &reply.sfFile.name);
- windowName[0] = strlen(&windowName[1]);
- tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
-
- SetPort(tempWindow);
- SetWRefCon(tempWindow, (long)dataHandle);
- HUnlock(dataHandle);
-
- { CTabHandle colorTable;
- colorTable = GetCTable(standardClutID);
- originalPalette = NewPalette(colorsToRequest, colorTable, pmTolerant, 0);
- NSetPalette(tempWindow, originalPalette, pmFgUpdates);
- ActivatePalette(tempWindow);
- }
-
- /* this is using the median method */
- dataHandle = (windowData **)NewHandle(sizeof(windowData));
- HLock(dataHandle);
- (*dataHandle)->contentPicture = CloneDiskPicture(contentPicture);
-
- sprintf(&windowName[1], "%#s : median", &reply.sfFile.name);
- windowName[0] = strlen(&windowName[1]);
- tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
-
- SetPort(tempWindow);
- SetWRefCon(tempWindow, (long)dataHandle);
- HUnlock(dataHandle);
-
- { long original;
- original = TickCount();
- GetDiskPictureInfo(contentPicture, &resultInfo, returnPalette, colorsToRequest, medianMethod);
- original = TickCount() - original;
- }
- NSetPalette(tempWindow, resultInfo.thePalette, pmFgUpdates);
- ActivatePalette(tempWindow);
-
-
- /* this is using the popular method */
-
- dataHandle = (windowData **)NewHandle(sizeof(windowData));
- HLock(dataHandle);
- (*dataHandle)->contentPicture = CloneDiskPicture(contentPicture);
-
- sprintf(&windowName[1], "%#s : popular", &reply.sfFile.name);
- windowName[0] = strlen(&windowName[1]);
- tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
-
- SetPort(tempWindow);
- SetWRefCon(tempWindow, (long)dataHandle);
- HUnlock(dataHandle);
-
- { long original;
- original = TickCount();
- GetDiskPictureInfo(contentPicture, &resultInfo, returnPalette, colorsToRequest, popularMethod);
- original = TickCount() - original;
- }
- NSetPalette(tempWindow, resultInfo.thePalette, pmFgUpdates);
- ActivatePalette(tempWindow);
-
- /* this is using the octree method */
- dataHandle = (windowData **)NewHandle(sizeof(windowData));
- HLock(dataHandle);
- (*dataHandle)->contentPicture = CloneDiskPicture(contentPicture);
-
- sprintf(&windowName[1], "%#s : octree", &reply.sfFile.name);
- windowName[0] = strlen(&windowName[1]);
- tempWindow = NewCWindow(nil, &windowBounds, &windowName[0], true, noGrowDocProc, (void *)(-1), true, 0);
-
- SetPort(tempWindow);
- SetWRefCon(tempWindow, (long)dataHandle);
- HUnlock(dataHandle);
-
- { long original;
- original = TickCount();
- GetDiskPictureInfo(contentPicture, &resultInfo, returnPalette, colorsToRequest, octreeMethod);
- original = TickCount() - original;
- }
- NSetPalette(tempWindow, resultInfo.thePalette, pmFgUpdates);
- ActivatePalette(tempWindow);
- }
-
-
- void SetUpMenus(void)
- {
- short index;
-
- myMenus[appleM] = GetMenu(appleID);
- AddResMenu(myMenus[appleM], 'DRVR');
- myMenus[fileM] = GetMenu(fileID);
- myMenus[editM] = GetMenu(editID);
-
- for(index=appleM; index <= editM; index++)
- InsertMenu(myMenus[index], 0) ;
-
- DrawMenuBar();
- }
-
-
- void SetUpWindows(void)
- {
- }
-
-
- void MaintainMenus(void)
- {
- }
-
-
- void UpdateWindow(WindowPtr theWindow)
- {
- GrafPtr savePort;
- windowData **dataHandle = (windowData **)GetWRefCon(theWindow);
-
- GetPort(&savePort);
- SetPort(theWindow);
-
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- DrawControls(theWindow);
-
- DrawDiskPicture((*dataHandle)->contentPicture);
-
- EndUpdate(theWindow);
-
- SetPort(savePort);
- }
-
-
- void CantOpen(void)
- {
- Rect r;
- WindowPtr tempWindow;
-
- SetRect(&r, 152, 60, 356, 132);
- SetPort((tempWindow = NewWindow( (Ptr) 0L, &r, "\p", true, dBoxProc, (WindowPtr) -1L, false, 0L)));
- TextFont(0);
- MoveTo(4, 20);
- DrawString("\pCan't open resource file.");
- MoveTo(4, 40);
- DrawString("\pClick mouse to exit.");
- do {
- } while (!Button());
- }
-